package Client; //done

import Common.Settings;
import GUI.LoginMenu;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * This is a abstract class to all main panels for clients use for logOF button
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public abstract class ClientAbstractWindow extends JPanel{
	
	/** The serial Version UID. */
	private static final long serialVersionUID = 3434830013656310168L;
	
	/** The LogOut Button. */
	private JButton LogOutButton = null;
	
	/** The user name Label. */
	private JLabel usernameLabel = null;
	
	/**
	 * This is the default constructor
	 */
	public ClientAbstractWindow(){
		super();
		setBackground(SystemColor.activeCaption);
		initialize();	
	}
	
	/**
	 * This function initializes ClientAbstractWindow
	 */
	private void initialize(){
		this.setSize(600, 300);
		this.setLayout(null);
		this.add(getActiveCampaignButton());
		
		JLabel imglabel = new JLabel("");
		imglabel.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/Logout-icon.png")));
		imglabel.setBounds(476, 23, 124, 146);
		add(imglabel);
		
		usernameLabel = new JLabel();
		usernameLabel.setText("Welcome ");
		usernameLabel.setFont(new Font("Tahoma", Font.BOLD, 13));
		usernameLabel.setBounds(476, 23, 124, 23);
		this.add(usernameLabel, null);
		
		JLabel lblWelcome = new JLabel("Welcome " + Settings.Logged_Username);
		lblWelcome.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblWelcome.setBounds(476, 23, 124, 23);
		add(lblWelcome);
	}
	
	/**
	 * This function initializes UsernameLabel	
	 */
	public JLabel getUsernameLabel(){

		return usernameLabel;
	}
	
	/**
	 * This function initializes LogOutButton	
	 */
	private JButton getActiveCampaignButton() {
		if (LogOutButton == null) {
			LogOutButton = new JButton();
			LogOutButton.setBounds(new Rectangle(489, 80, 101, 29));
			LogOutButton.setText("Log Out");
			LogOutButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {			
					try{
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						String loggedofmsg = (String)connectiontoserver.LogOut();
						if(loggedofmsg != null && loggedofmsg.equals("LoggedOut")){
							JOptionPane.showMessageDialog(null, "Logged out successfuly");
							System.exit(0);
						}
						else
							JOptionPane.showMessageDialog(null, "Error!","",JOptionPane.WARNING_MESSAGE);
					} 
					catch (Exception e1){
						System.exit(0);
					}					
				}
			});
		}
		return LogOutButton;
	}
}
